import axios from 'axios'
import { ElMessageBox, ElMessage } from 'element-plus'
const ERRORMES = '服务器连接失败，请联系管理员!'
import config  from '../config';
console.log('当前的配置文件', config)
const instance = axios.create({
  baseURL: config.mock ? config.mockUrl : config.baseURL,
  timeout: 2000,
});


instance.interceptors.request.use(config => {
  // 请求拦截器   
  return config
})

instance.interceptors.response.use(config => {
  // 响应拦截器
  console.log('当前code是', config)
  const { status: code, data } =  config
  if (code !== 200) {
    if (code == 401 || code == 402) {
      ElMessageBox.alert('用户信息已过期，请重新登录', '提示', {
        confirmButtonText: 'OK',
        callback: (action) => {
          ElMessage({
            type: 'warning',
            message: `action: ${action}`,
          })
        },
      })
    } else if (code == 500) {
      ElMessage.error(ERRORMES)
      return Promise.reject(ERRORMES)
    } else {
      ElMessage.error('保错了')
      return Promise.reject('报错了')
    }
  } 
  return data
}, error => {
  console.log(error)
  ElMessage.error(ERRORMES)
  return Promise.reject(ERRORMES)
})


export default instance